package com.uyaer.wheel.display.widget
{
	import com.uyaer.wheel.common.Common;
	import com.uyaer.wheel.res.ResManager;
	
	import starling.animation.Transitions;
	import starling.core.Starling;
	import starling.display.Image;
	import starling.display.Quad;
	import starling.display.Sprite;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;
	import starling.filters.BlurFilter;
	import starling.text.TextField;
	import starling.utils.VAlign;
	
	/**
	 *确认提示 
	 * @author hc
	 * 
	 */	
	public class Alert extends Sprite
	{
		private var yesFun:Function;
		private var mask:Quad;
		private var con:Sprite;
		private var infoTxt:TextField;
		private var yesBtn:Quad;
		private var noBtn:Quad;
		private var c_dark:uint = 0x4c5258;
		private var c_light:uint = 0x5397d8;
		private var blur:BlurFilter;
		
		public function Alert(){
			blur = BlurFilter.createDropShadow(6);
			layout();
			this.addEventListener(TouchEvent.TOUCH,onTouch);
		}
		
		private function onTouch(e:TouchEvent):void{
			var touch:Touch = e.getTouch(this);
			if(touch){
				if(touch.phase == TouchPhase.BEGAN){
					var q:Quad = (e.target as Quad);
					if(q)q.color = c_light;
				}else if(touch.phase == TouchPhase.ENDED){
					switch(e.target){
						case yesBtn:
							hide(yesFun);
							break;
						case noBtn:
							hide();
							break;
					}
				}
			}
		}
		
		private function hide(callback:Function = null):void{
			con.filter = null;
			var h:Number = Common.H+con.height;
			Starling.juggler.tween(con,0.3,{"y":h,"transition":Transitions.EASE_IN,
						"onComplete":hideOver,"onCompleteArgs":[callback]});
		}
		
		private function hideOver(callback:Function):void{
			this.removeFromParent();
			callback && callback();
		}
		
		private function layout():void{
			mask = new Quad(Common.W,Common.H,0x0);
			mask.alpha = 0.8;
			mask.touchable = false;
			addChild(mask);
			
			var w:Number = Math.min(400,Common.W * 0.8);
			var h:Number = w * 0.618;
			var bh:Number = Math.min(h *0.3,58);
			
			con = new Sprite();
			addChild(con);
			var q:Quad = new Quad(w,h,c_dark);
			con.addChild(q);
			var titleTF:TextField = new TextField(w-20,bh-5,"提示");
			titleTF.vAlign = VAlign.BOTTOM;
			titleTF.fontSize = 28;
			titleTF.color = c_light;
			titleTF.x = 10;
			con.addChild(titleTF);
			var ticon:Image = new Image(ResManager.me.getTexture("menu_alert_bulb"));
			ticon.x = titleTF.textBounds.left -ticon.width-3;
			ticon.pivotY = ticon.height >>1;
			ticon.y = bh>>1;
			con.addChild(ticon);
			
			q = new Quad(w,2,c_light);
			q.y = bh+2;
			con.addChild(q);
			
			infoTxt = new TextField(w-20,h-80,"确定要关闭应用？");
			infoTxt.fontSize = 22;
			infoTxt.color = 0xffffff;
			infoTxt.x = 10;
			infoTxt.y = 40;
			con.addChild(infoTxt);
			
			q = new Quad(w-2,bh+1,0x979899);
			q.pivotY = bh+1;
			q.x = 1;
			q.y = h-1;
			con.addChild(q);
			
			yesBtn = new Quad(w*0.5-1.5,bh,c_dark);
			noBtn = new Quad(w*0.5-1.5,bh,c_dark);
			con.addChild(yesBtn);
			con.addChild(noBtn);
			yesBtn.x = 1;
			noBtn.x = w * 0.5+0.5;
			yesBtn.pivotY = bh;
			noBtn.pivotY = bh;
			yesBtn.y = noBtn.y = h -1;
			var yesTF:TextField = new TextField(yesBtn.width,yesBtn.height,"确定");
			yesTF.color = 0xffffff;
			yesTF.fontSize = 22; 
			yesTF.x = yesBtn.x; 
			yesTF.y = yesBtn.y;
			yesTF.pivotY = yesBtn.pivotY;
			con.addChild(yesTF);
			var noTF:TextField = new TextField(noBtn.width,noBtn.height,"取消");
			noTF.color = 0xffffff;
			noTF.fontSize = 22; 
			noTF.x = noBtn.x; 
			noTF.y = noBtn.y;
			noTF.pivotY = noBtn.pivotY;
			con.addChild(noTF);
			yesTF.touchable = false;
			noTF.touchable = false;
			
			con.pivotX = con.width >>1;
			con.pivotY = con.height >>1;
			con.x = Common.W >> 1;
		}
		/**
		 *显示确认取消按钮 
		 * @param info
		 * @param yesCallback
		 * @param noCallback
		 * 
		 */		
		public function showYesNo(info:String,yesCallback:Function=null,noCallback:Function=null):void{
			yesFun = yesCallback;
			infoTxt.text = info;
			yesBtn.color = c_dark;
			noBtn.color = c_dark;
			con.y = Common.H+con.height;
			Starling.juggler.tween(con,0.65,{"y":Common.H >>1,"transition":Transitions.EASE_OUT,
						"onComplete":function():void{con.filter = blur;}});
		}
		
		/**
		 *显示只有一个按钮的确认框框 
		 * @param info
		 * @param yesCallback
		 * 
		 */		
		public function showYes(info:String,yesCallback:Function=null):void{
			
		}
		
		/**
		 *android中的提示(!?是否调用ane，调用本机确认？) 
		 * @param info
		 * 
		 */		
		public function toast(info:String):void{
			
		}
	}
}